package cn.ococci.dm.Main.Tools;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import cn.ococci.dm.Main.Model.DoorModel;
import cn.ococci.dm.Main.Model.DoorUnitModel;
import cn.ococci.dm.Main.Model.Ota;
import cn.ococci.dm.Main.Model.UnitModel;
import cn.ococci.dm.Main.Model.User;
import cn.ococci.dm.Main.Model.WifiObj;


/**
 * Created by cyan on 15/9/22.
 * 数据库工具类，这里后期需要加入当用户切换了的时候，那么必须清除数据库中的数据
 */
public class DbTool {


    /**
     * 清除所有数据库中表的数据
     */
    public static void clearDb(){
        DataSupport.deleteAll(User.class);
        DataSupport.deleteAll(DoorModel.class);
        DataSupport.deleteAll(UnitModel.class);
        DataSupport.deleteAll(DoorUnitModel.class);
    }


    /**
     * 查找获取当前用户
     * @return
     */
    public static User getCurUser(){
        return DataSupport.findFirst(User.class);
    }

    /**
     * 获取当前管理员所有的门禁
     * @return
     */
    public static List<DoorModel> findAllDoor(){

        List<DoorModel> doorList = DataSupport.findAll(DoorModel.class);

        return  doorList;

    }


    public static List<UnitModel> findAllUnitModel(){

        List<UnitModel> unitModels = DataSupport.findAll(UnitModel.class);

        return unitModels;

    }



    /**
     * 根据doorId 查询出所有需要上传的楼栋单元数据
     * @param doorId
     * @return
     */
    public static List<DoorUnitModel> findDoorUnitListWithNeedSyn(long doorId){

        List<DoorUnitModel>  modelList =  DataSupport.where("doorId = ? and syn = 0",""+doorId).find(DoorUnitModel.class);

        return modelList;

    }


    /**
     * 查询出还没有上传的WiFiObj
     *
     * @return
     */
    public static List<WifiObj> findAllNoUploadsWifiObjData(){

        List<WifiObj> data = DataSupport.findAll(WifiObj.class);

        if(StringTool.hasData(data)) {

            return data;
        }

        return  new ArrayList<>();
    }


    /**
     * 查询单个OTA
     * @param otaType
     * @return
     */
    public static Ota findOTA(int otaType){

        List<Ota> otaList = DataSupport.where("otaType = ?",""+otaType).find(Ota.class);

        if(StringTool.hasData(otaList)){
            return otaList.get(0);
        }

        return new Ota();
    }


}
